package com.crm.consult.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crm.model.ConsultInfoManage;
import com.crm.model.SysDepartment;
import com.crm.model.SysDuty;
import com.crm.model.SysUser;
import com.crm.model.entity.CountWorkBench;
import com.crm.model.entity.KeyValue;
import com.crm.service.ConsultInfoManageService;
import com.crm.service.SysDepartmentService;
import com.crm.service.SysDutyService;
import com.crm.service.SysUserService;
import com.crm.util.JsonUtil;
import com.crm.util.ResponseUtil;
import com.crm.util.SessionModel;
import com.crm.util.SessionUtils;
import com.github.pagehelper.Page;

@Controller
@RequestMapping("/consultInfoManage")
public class ConsultInfoManageController {
	
	@Autowired
	private ConsultInfoManageService consultInfoManageService;
	@Autowired
	private SysDutyService sysDutyService;
	@Autowired
	private SysDepartmentService sysDepartmentService;
	@Autowired
	private SysUserService userService;
	
	@RequestMapping("/index")
	public String index(){
		
		return "consultcenter/infoManagement";
	}
	
	@RequestMapping("/getAll")
	@ResponseBody
	public void getAll(HttpServletResponse response,HttpServletRequest request, Page<ConsultInfoManage> page, ConsultInfoManage t){
		
		Page<ConsultInfoManage> list = (Page<ConsultInfoManage>) consultInfoManageService.selectAll(page, t);
		
		ResponseUtil.resultPage(response,request,list);
	}
	
	@RequestMapping("/addNewRecord")
	@ResponseBody
	public Map<String, Object> addNewRecord(ConsultInfoManage record){
		
		String conversation = record.getConversation();
		
		conversation = conversation.replace("&quot", "");
		
		record.setConversation(conversation);
		
		//通过项目ID，分校ID，品牌ID，确定信息量归属部门
		record  = consultInfoManageService.getDepartment2Id(record);
		
		//根据信息量归属部门，确定部门相关类型
		SysDepartment sd = sysDepartmentService.selectByPrimaryKey(record.getDepartmentId2());
		if(sd.getType()==3){
			record.setTypeFrom("3");
		}else{
			record.setTypeFrom("1");
		}
//		SysUser sy = (SysUser)SessionUtils.get(SessionModel.SESSION_USER);
		
		ConsultInfoManage cim = new ConsultInfoManage();
//		cim.setDepartmentId2(sy.getDepartmentId());
		cim = consultInfoManageService.getTopOne(record);
		if(cim!=null){
			if(cim.getCounselorId()!=null){
				record.setCounselorId(cim.getCounselorId());
				record.setCounselor(cim.getCounselor());
				record.setStatus(new Byte("2"));
			}else{
				record.setStatus(new Byte("1"));
			}
		}
		
		 Map<String, Object> map = new HashMap<>();
		
		consultInfoManageService.insert(record);
		
		map.put("status", "success");
		map.put("msg", "新增成功！");
		
		return map;
	}
	
	@RequestMapping("/deleteRecord")
	@ResponseBody
	public Map<String, Object> deleteRecord(ConsultInfoManage record){
		
		Map<String, Object> map = new HashMap<>();
		
		record.setDeleteMark(0);
		consultInfoManageService.updateByPrimaryKey(record);
		
		map.put("status", "success");
		map.put("msg", "删除成功！");
		
		return map;
	}
	
	@RequestMapping("/updateRecord")
	@ResponseBody
	public Map<String, Object> updateRecord(ConsultInfoManage record){
		SysUser sy = ((SysUser)SessionUtils.get(SessionModel.SESSION_USER));
		Map<String, Object> map = new HashMap<>();
		
		if(record.getBMCode()==null){
			record.setBMCode("BM"+System.nanoTime()/1000);
		}
		if(record.getStatus()!=null){
			if(record.getStatus()==6){
				record.setDingZDate(new Date());
				record.setReciveId(sy.getUserId());
				record.setReciveName(sy.getRealName());
			}
			if(record.getStatus()==7){
				record.setBaoMDate(new Date());
				record.setReciveId(sy.getUserId());
				record.setReciveName(sy.getRealName());
			}
		}
		
		consultInfoManageService.updateByPrimaryKey(record);
		
		map.put("status", "success");
		map.put("msg", "修改成功！");
		
		return map;
	}
	
	/**
	 * 
	 * @Description : 分拨中心加载数据
	 * @author zealot
	 * @param @param response
	 * @param @param request
	 * @param @param page
	 * @param @param t
	 * @return void
	 * @version 创建时间 : 2016年11月25日 上午11:37:05
	 * @thorws
	 */
	@RequestMapping("/load")
	@ResponseBody
	public void load(HttpServletResponse response,HttpServletRequest request, Page<ConsultInfoManage> page, ConsultInfoManage t){
		if(!"0".equals(t.getCatRolEnable())){
			SysUser sy = ((SysUser)SessionUtils.get(SessionModel.SESSION_USER));
			t.setCatRol(sy.getSecurityLevel());
			t.setCounselorId(sy.getUserId());
			SysDuty sd =  sysDutyService.selectByPrimaryKey(sy.getDutyId());
			if(sd!=null){
				if("11".equals(sd.getCode())||"10".equals(sd.getCode())){
					t.setRoleFW("1");
					t.setDepartmentId1(sy.getCompanyId());
				}else{
					t.setRoleFW("2");
					t.setDepartmentId1(sy.getDepartmentId());
				}
			}else{
				t.setDepartmentId1(sy.getCompanyId());
			}
		}
		Page<ConsultInfoManage> list = (Page<ConsultInfoManage>) consultInfoManageService.selectAll(page, t);
		
		SysUser user = (SysUser) SessionUtils.get(SessionModel.SESSION_USER);
		
		request.setAttribute("user", user);
		
		ResponseUtil.resultPage(response,request,list);
	}
	
	/**
	 * 
	 * @Description : 信息统计汇总
	 * @author zealot
	 * @param @param response
	 * @param @param request
	 * @param @param t
	 * @return void
	 * @version 创建时间 : 2016年12月9日 上午11:39:13
	 * @thorws
	 */
	@RequestMapping("/loadSum")
	@ResponseBody
	public void loadSum(HttpServletResponse response,HttpServletRequest request,ConsultInfoManage t){
		if(!"0".equals(t.getCatRolEnable())){
			SysUser sy = ((SysUser)SessionUtils.get(SessionModel.SESSION_USER));
			t.setCatRol(sy.getSecurityLevel());
			t.setCounselorId(sy.getUserId());
			SysDuty sd =  sysDutyService.selectByPrimaryKey(sy.getDutyId());
			if(sd!=null){
				if("11".equals(sd.getCode())||"10".equals(sd.getCode())){
					t.setRoleFW("1");
					t.setDepartmentId1(sy.getCompanyId());
				}else{
					t.setRoleFW("2");
					t.setDepartmentId1(sy.getDepartmentId());
				}
			}else{
				t.setDepartmentId1(sy.getCompanyId());
			}
		}
		List<KeyValue> list = consultInfoManageService.loadSum(t);
		ResponseUtil.result(response, JsonUtil.toJsonStr(list));
	} 
	

	
	
	//依据品牌和项目验证是否已经录入
	@RequestMapping("/getAllOption")
	@ResponseBody
	public Map<String, Object> getAllOption(Page<ConsultInfoManage> page, ConsultInfoManage t){
		Map<String, Object> map = new HashMap<>();
		Page<ConsultInfoManage> list = (Page<ConsultInfoManage>) consultInfoManageService.selectView(page, t);
		map.put("list", list);
		map.put("status", "success");
		map.put("msg", "查询成功！");
		return map;
	}
	
	@RequestMapping("/loadM")
	@ResponseBody
	public void loadM(HttpServletResponse response,HttpServletRequest request, Page<ConsultInfoManage> page, ConsultInfoManage t){
		SysUser sy = ((SysUser)SessionUtils.get(SessionModel.SESSION_USER));
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		if("呼入咨询师".equals(sy.getDutyName())){
			map.put("userId", sy.getUserId());
		}
		
		map.put("departmentId", sy.getDepartmentId());
		map.put("companyId", sy.getCompanyId());
		map.put("dutyId", sy.getDutyId());
		map.put("beginTime", t.getBeginTime());
		map.put("endTime", t.getEndTime());
		map.put("searchVal", t.getSearchVal());
			
		Page<ConsultInfoManage> list = (Page<ConsultInfoManage>) consultInfoManageService.selectAllM(page, map);
		
		ResponseUtil.resultPage(response,request,list);
	}
	
	@RequestMapping("/loadD")
	@ResponseBody
	public void loadD(HttpServletResponse response,HttpServletRequest request, Page<ConsultInfoManage> page, ConsultInfoManage t){
		SysUser sy = ((SysUser)SessionUtils.get(SessionModel.SESSION_USER));
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		//map.put("departmentId2", sy.getDepartmentId());
		map.put("companyId", sy.getCompanyId());
		map.put("dutyId", sy.getDutyId());
		map.put("beginTime", t.getBeginTime());
		map.put("endTime", t.getEndTime());
		map.put("searchVal", t.getSearchVal());
		
		Page<ConsultInfoManage> list = (Page<ConsultInfoManage>) consultInfoManageService.selectAllD(page, map);
		
		ResponseUtil.resultPage(response,request,list);
	}
	
	/**
	 * 
	 * @Description : 检索欠费总计
	 * @author zealot
	 * @param @param response
	 * @param @param request
	 * @param @param t
	 * @return void
	 * @version 创建时间 : 2017年1月12日 上午9:39:59
	 * @thorws
	 */
	@RequestMapping("/ajaxLoadMustPaySum")
	@ResponseBody
	public void ajaxLoadMustPaySum(HttpServletResponse response,HttpServletRequest request,ConsultInfoManage t){
			SysUser sy = ((SysUser)SessionUtils.get(SessionModel.SESSION_USER));
			t.setCatRol(sy.getSecurityLevel());
			t.setCounselorId(sy.getUserId());
			SysDuty sd =  sysDutyService.selectByPrimaryKey(sy.getDutyId());
			if(sd!=null){
				if("11".equals(sd.getCode())||"10".equals(sd.getCode())){
					t.setRoleFW("1");
					t.setDepartmentId1(sy.getCompanyId());
				}else{
					t.setRoleFW("2");
					t.setDepartmentId1(sy.getDepartmentId());
				}
			}else{
				t.setDepartmentId1(sy.getCompanyId());
			}
		ResponseUtil.result(response, JsonUtil.toJsonStr(consultInfoManageService.loadMustPaySum(t)));
	}
}

